package edu.ctbu.insurance.service.impl;

import com.github.pagehelper.PageHelper;
import edu.ctbu.insurance.bean.Claim;
import edu.ctbu.insurance.mapper.ClaimMapper;
import edu.ctbu.insurance.service.ClaimService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author djl
 * @since 2024-06-28
 */
@Service
@Transactional
public class ClaimServiceImpl extends ServiceImpl<ClaimMapper, Claim> implements ClaimService {
    @Autowired
    private ClaimMapper claimMapper;

    @Override
    public List<Claim> findAllClaim(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return claimMapper.selectList(null);
    }

    @Override
    public List<Claim> findAllClaimLikeClaimIdAndPolicyId(String claimId, String policyId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return claimMapper.findAllClaimLikeClaimIdAndPolicyId(claimId, policyId);
    }

    @Override
    public void updatePassClaimStatusAndSteps(String claimId) {
        Claim claim = claimMapper.selectById(claimId);
        claim.setSteps(claim.getSteps()+1);
        if (claim.getSteps() == 6) {
            claim.setStatus("success");
        }
        claimMapper.updateById(claim);
    }

    @Override
    public void updateRejectClaimStatusAndSteps(String claimId) {
        Claim claim = claimMapper.selectById(claimId);
        claim.setStatus("error");
        claimMapper.updateById(claim);
    }
}
